#-*- coding: utf-8 -*-
import json
from util.Location import Location
from util.Metadata import Metadata

'''
Converte una stringa JSON in un Dizionario di Location

@param json_str: Stringa che contiene le locationnin formato JSON
@return: Dizionario che associa Oggetti di tipo Location ai relativi id
@author: Simone Rondelli
'''
def parse_json(json_str):
    parsed = json.loads(json_str)
    locations = {}
    
    metadata = Metadata()
    metaj = parsed['metadata']
    metadata.created = metaj['created']
    metadata.creator = metaj['creator']
    metadata.source = metaj['source']
    metadata.valid = metaj['valid']
    metadata.version = metaj['version']
    
    for k in parsed['locations'].keys():
        try:   
            l = parsed['locations'][k]
            tmpLocation = Location()
            tmpLocation.id = k
            tmpLocation.address = l['address']
            tmpLocation.category = str(l['category'][0])
            tmpLocation.closing = l['closing']
            tmpLocation.lat = float(l['lat'])
            tmpLocation.long = float(l['long'])
            tmpLocation.name = l['name']
            tmpLocation.opening = l['opening']
            if l.get('tel') != None:
                tmpLocation.tel = l['tel']
            if l.get('note') != None:
                tmpLocation.note = l['note']
            if l.get('subcategory') != None:
                tmpLocation.subcategory = l['subcategory']
                
            tmpLocation.metadata = metadata
            locations[k] = tmpLocation
        except Exception as ex:
            print ex
    
    return locations

'''
Dato un Dizionario di Location crea il relativo json

@param locations_dict: Dizionario che associa Oggetti di tipo Location ai relativi id
@return: Stringa che contiene il risultato in json
@author: Simone Rondelli
'''
def write_json(locations_dict):
    json = '{\n'
    json += '\t"locations": {\n'
    index = 0
    metadata = None
    
    for k in locations_dict.keys():
        loc = locations_dict[k]
    
        json += '\t\t"' + k + '": {\n'
        json += '\t\t\t"category": ["' + loc.category + '"],\n'
        json += '\t\t\t"name": "' + loc.name.replace('"', '\\"') + '",\n'
        json += '\t\t\t"address": "' + loc.address + '",\n'
        json += '\t\t\t"lat": "' + str(loc.lat) + '",\n'
        json += '\t\t\t"long": "' + str(loc.long) + '",\n'
        
        if loc.tel:
            json += '\t\t\t"tel": "' + loc.tel + '",\n'
        if loc.note:
            json += '\t\t\t"note": "' + loc.note + '",\n'   
        if loc.subcategory:
            json += '\t\t\t"subcategory": "' + loc.subcategory + '",\n'
        
        json += '\t\t\t"opening": "' + loc.opening + '",\n'
        json += '\t\t\t"closing": "' + loc.closing + '"\n'
            
        index += 1
        
        if len(locations_dict) != index:
            json += '\t\t},\n'
        else:
            json += '\t\t}\n'
            metadata = loc.metadata
    

    if metadata:
        json += '\t},\n'
        json += '\t"metadata": {\n'
        json += '\t\t"creator": "' + metadata.creator + '",\n'
        json += '\t\t"created": "' + metadata.created + '",\n'
        json += '\t\t"version": "' + metadata.version + '",\n'
        json += '\t\t"source": "' + metadata.source + '",\n'
        json += '\t\t"valid": "' + metadata.valid + '"\n'

        
    json += '\t}\n'
    json += '}\n'
    
    if type(json) == str:
        json = unicode(json,'utf8')
    
    return json

